﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Warensoft.Utility;
using Warensoft.Socket;
using System.IO;
using System.Reflection;
using System.Threading;
using Warensoft.PythonExtension;
namespace Warensoft.SocketImplementation
{
    public class WarensoftSocketServer:Warensoft.Socket .SocketServer 
    {
        //private ServiceHost wcf = null;
        /// <summary>
        /// 读取配置文件
        /// </summary>
        private DynamicIpyFileLoader configLoader = new DynamicIpyFileLoader();
        /// <summary>
        /// 读取消息处理文件
        /// </summary>
        private DynamicIpyFolderLoader messageLoader = new DynamicIpyFolderLoader();
        public DynamicIpyFolderLoader MessageLoader
        {
            get
            {
                return this.messageLoader;
            }
        }
        private DynamicIpyFileLoader sessionPyLoader = new DynamicIpyFileLoader();
        public DynamicIpyFileLoader SessionPyLoader
        {
            get
            {
                return this.sessionPyLoader;
            }
        }
        /// <summary>
        /// 频道内功能实现文件
        /// </summary>
        private DynamicIpyFileLoader channelPyLoader = new DynamicIpyFileLoader();
        public DynamicIpyFileLoader ChannelPyLoader
        {
            get
            {
                return this.channelPyLoader;
            }
        }
        /// <summary>
        /// 启动条件处理文件
        /// </summary>
        private DynamicIpyFileLoader startCondationLoader = new DynamicIpyFileLoader();
        private DynamicIpyFileLoader serviceLoader = new DynamicIpyFileLoader();
        public WarensoftSocketServer()
        {
            try
            {
                Environment.CurrentDirectory = Directory.GetCurrentDirectory();
                var exePath = Assembly.GetExecutingAssembly().Location;
                var folder = Path.GetDirectoryName(exePath );
                //this.OnError("trace",new Exception (System .Windows.Forms.Application .StartupPath ));
                this.configLoader.FileName =folder+ @"\config\config.py";
                this.configLoader.ScriptLoaded += new EventHandler(configLoader_ScriptLoaded);
                this.messageLoader.PlugInFolder = folder + @"\messages";
                this.channelPyLoader.FileName = folder + @"\channel\channel.py";
                this.startCondationLoader.FileName = folder + @"\start\start.py";
                this.sessionPyLoader.FileName = folder + @"\session\session.py";
                this.serviceLoader.FileName = folder + @"\service\serviceapi.py";
            }
            catch (Exception ex)
            {
                this.OnError("server constructor",ex);
            }
           
        }
        /// <summary>
        /// 加载Python脚本
        /// </summary>
        public void LoadPython()
        {
            this.configLoader.Load();
            this.messageLoader.Load();
            this.channelPyLoader.Load();
            this.startCondationLoader.Load();
            this.sessionPyLoader.Load();
            this.serviceLoader.Load();
        }
        void configLoader_ScriptLoaded(object sender, EventArgs e)
        {
            this.IPAddress = System.Net.IPAddress.Parse(this.configLoader .CallbackObject .GetIP());
            this.Port = int.Parse(this.configLoader .CallbackObject .GetPort());
        }
        public override Warensoft.Socket.ClientSessionBase OnNewSession(Warensoft.Socket.ClientSocket socket)
        {
            var clientSession = new Session(socket);
            
            
            return clientSession;
        }

        public override ResultInfo CheckStartCondations()
        {
            ResultInfo result = new ResultInfo();
            try
            {
               result= this.startCondationLoader.CallbackObject.CheckStartCondations(this);
            }
            catch (Exception ex)
            {
                this.OnError ("Warensoft.Socket CheckStartCondations Error",ex);
                result.OperationDone =false ;
                result .Message =ex.Message ;
            }
            finally
            {

            }
            return result;
        }

        protected override ResultInfo<Warensoft.Socket.Channel> CreateChannel(string name)
        {
            ResultInfo<Channel> result = new ResultInfo<Channel>();
            var channel = this.GetChannel(name);
            result.OperationDone = true;
            if (channel == null)
            {
                result.OperationDone = false;
                channel = new WarensoftChannel(name, this.channelPyLoader.CallbackObject, this);
            }
            result.AdditionalData = channel;
            return result;
        }
       
        public override void OnStart()
        {
            try
            {
                this.LoadPython();
                this.StartService();
            }
            catch (Exception ex)
            {
                this.OnError("GameSocketServer.cs 148",ex);
            }
        }
        public void StartService()
        {
            this.serviceLoader.CallbackObject.Server = this;
            this.serviceLoader.CallbackObject.StartService();        
        }

        public override void OnStop()
        {
            this.serviceLoader.CallbackObject.CloseService();
        }
        
    }
}
